package com.guigu.controller;


import com.guigu.pojo.Cart;
import com.guigu.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 购物车表 前端控制器
 * </p>
 *
 * @author Mr.Zhang
 * @since 2025-03-12
 */
@CrossOrigin
@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    //根据用户id查询购物车
    @GetMapping("/getCartByConsumerId")
    @ResponseBody
    public Object getCartByConsumerId(Integer consumerId) {
        List<Cart> cartByConsumerId = cartService.getCartByConsumerId(consumerId);
        return cartByConsumerId;
    }

    @PostMapping("/addToCart")
    @ResponseBody
    public Boolean addToCart(Cart cart) {
        cart.setCreateTime(new Date());
        boolean save = cartService.addToCart(cart);
        return save;
    }

    @PutMapping("/updateCart")
    @ResponseBody
    public Boolean updateCart(@RequestBody List<Cart> carts) {
        boolean b = cartService.updateBatchById(carts);
        return b;
    }

    @DeleteMapping("/deleteCart")
    @ResponseBody
    public Boolean deleteCart(Integer cartId) {
        boolean b = cartService.removeById(cartId);
        return b;
    }


}

